<?php

namespace app\controller;


use app\service\OrderReviewService;
use app\service\OrderService;
use app\service\RefundRulesService;
use const support\ERROR;
use const support\JSAPI;
use const support\LIMIT;
use const support\order_process_status_0;
use const support\order_status_1;
use const support\PAGE;
use const support\PARAMETER_ERROR;

class OrderController
{

    //订单列表
    public function orderLists(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'page'	                =>PAGE,
            'limit'	                =>LIMIT,
            'order_process_status'  =>order_process_status_0
        ]);
        try {
            $info=OrderService::orderLists(getMemberId(),$params['order_process_status'],$params['page'],$params['limit']);
            return buildSuccess($info);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //订单详情
    public function orderDetails(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	            =>0
        ]);
        try {
            list($retCode,$msg,$data)=OrderService::orderDetails(getMemberId(),$params['order_id']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //申请开票
    public function orderInvoice(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	            =>0,
            'type'                  =>0,
            'company_name'          =>'',
            'company_number'        =>'',
            'company_address'       =>'',
            'company_tel'           =>'',
            'company_open_bank'     =>'',
            'consignee_name'        =>'',
            'consignee_mobile'      =>'',
            'consignee_address'     =>''
        ]);
        try {
            list($retCode,$msg,$data)=OrderService::toOrderInvoice(getMemberId(),$params);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //申请开票历史记录
    public function orderInvoiceHistory(): \support\Response
    {
        try {
            $history=OrderService::getOrderInvoiceHistory(getMemberId());
            return buildSuccess($history);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //重新支付
    public function orderResetPay(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	        =>0,
            'trade_type'        =>JSAPI
        ]);
        try {
            list($retCode,$msg,$data)=OrderService::orderResetPay(getMemberId(),$params['order_id'],$params['trade_type']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //申请退款
    public function orderApplyRefund(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	        =>0,
            'order_refund_desc' =>''
        ]);
        try {
            list($retCode,$msg,$data)=OrderService::toApplyRefund(getMemberId(),$params['order_id'],$params['order_refund_desc']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //售后申请关联信息
    public function orderRefundRelation(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	        =>0
        ]);
        try {
            //获取选中的订单信息
            $order=OrderService::getOrderInfo($params['order_id']);
            if (empty($order)){
                return buildFail(PARAMETER_ERROR,'订单无效');
            }
            if ($order['is_refund']!==1 || $order['order_status']!==order_status_1){
                return buildFail(PARAMETER_ERROR,'该订单不符合退款条件');
            }
            //获取本次退款信息
            $refund=RefundRulesService::refundAlgorithmV1($order['order_actual_amount'],$order['goods_period_date']);
            $refund['goods_icon']=$order['goods_icon'];
            $refund['goods_title']=$order['goods_title'];
            return buildSuccess($refund);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //售后详情
    public function orderRefundDetails(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	            =>0
        ]);
        try {
            list($retCode,$msg,$data)=OrderService::orderRefundDetails(getMemberId(),$params['order_id']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //退款进度
    public static function orderRefundProgress(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	            =>0
        ]);
        try {
            list($retCode,$msg,$data)=OrderService::orderRefundProgress(getMemberId(),$params['order_id']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //商品下单
    public function orderToPay(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'goods_id'          =>'',
            'goods_period_id'	=>'',
            'specifications'    =>'',
            'joins'             =>'',
            'order_remark'      =>'',
            'trade_type'        =>JSAPI
        ],['specifications']);
        try {
            //数据转换
            $params['specifications']=is_string($params['specifications'])?json_decode($params['specifications'],true):[];
            $params['joins']=explode(',',$params['joins']);
            list($retCode,$msg,$data)=OrderService::orderToPayCalculate(getMemberId(),$params);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //商品预下单
    public function orderToPrePay(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'goods_id'          =>'',
            'goods_period_id'	=>'',
            'specifications'    =>''
        ],['specifications']);
        try {
            //数据转换
            $params['specifications']=json_decode($params['specifications'],true);
            list($retCode,$msg,$data)=OrderService::orderToPrePayCalculate($params);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //发表评论
    public function orderToComment(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'order_id'	            =>0,
            'goods_score'           =>5,
            'goods_comment'         =>'',
            'goods_comment_images'  =>''
        ]);
        try {
            list($retCode,$msg,$data)=OrderReviewService::toAddOrderComment(getMemberId(),$params['order_id'],$params['goods_score'],$params['goods_comment'],$params['goods_comment_images']);
            if ($retCode){
                return buildFail($retCode,$msg);
            }
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

}